package com.liuqi.aiagent.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.liuqi.aiagent.mapper.AgentEventMapper;
import com.liuqi.aiagent.mapper.ModelMapper;
import com.liuqi.aiagent.service.AgentEventService;
import com.liuqi.aiagent.service.ModelService;
import com.liuqi.aiagent.transfer.ApiResult;
import com.liuqi.aiagent.transfer.dto.ModelEditDto;
import com.liuqi.aiagent.transfer.dto.ModelSaveDto;
import com.liuqi.aiagent.transfer.entity.Model;
import com.liuqi.aiagent.transfer.mapstruct.ModelMapStruct;
import com.liuqi.aiagent.transfer.model.Result;
import com.liuqi.aiagent.transfer.model.ResultList;
import com.liuqi.aiagent.transfer.vo.ModelVo;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liuqi
 * @date 2025/7/15
 **/
@Service
@Slf4j
@AllArgsConstructor
public class ModelServiceImpl implements ModelService {

    private final ModelMapper modelMapper;

    private final AgentEventService agentEventService;

    private final AgentEventMapper agentEventMapper;

    @Override
    public Result save(ModelSaveDto modelSaveDto) {
        if (modelMapper.findByName(modelSaveDto.getName()) != null) {
            return ApiResult.fail("名称已存在");
        }
        log.info("添加 AI 模型. " + JSONObject.toJSONString(modelSaveDto));
        modelMapper.save(ModelMapStruct.INSTANCE.from(modelSaveDto));
        return ApiResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result edit(ModelEditDto modelEditDto) {
        Model model = ModelMapStruct.INSTANCE.from(modelEditDto);
        log.info("编辑 AI 模型. " + JSONObject.toJSONString(modelEditDto));

        // 编辑
        modelMapper.edit(model);

        model = modelMapper.findById(modelEditDto.getId());
        if (model.getType().equals("LLM")) {
            // 发布模型变更事件
            agentEventService.modelEditEvent(modelEditDto.getId());
        }

        return ApiResult.success();
    }

    @Override
    public Result delete(int id) {
        Model model = modelMapper.findById(id);

        if ("LLM".equals(model.getType())) {
            List<Integer> agentIds = agentEventMapper.findAgentIdByModelId(id);
            if (!agentIds.isEmpty()) {
                return ApiResult.fail("模型已被Agent关联, 先解除关联后删除");
            }
        }

        log.warn("删除AI模型, 模型id[{}]", id);
        modelMapper.delete(id);
        return ApiResult.success();
    }

    @Override
    public ResultList<ModelVo> findAll() {
        List<Model> models = modelMapper.findAll();
        List<ModelVo> modelVos = ModelMapStruct.INSTANCE.toVos(models);
        return ApiResult.success(modelVos, modelVos.size());
    }

    @Override
    public ResultList<ModelVo> findAllByType(String type) {
        List<Model> models = modelMapper.findAllByType(type);
        List<ModelVo> modelVos = ModelMapStruct.INSTANCE.toVos(models);
        return ApiResult.success(modelVos, modelVos.size());
    }
}
